import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;


public class NoteBoardImpl implements NoteBoard {
	private StringBuffer buf;
        private Vector registeredClients;
        
	public NoteBoardImpl() throws RemoteException {
		buf = new StringBuffer();
                registeredClients = new Vector();
	}

	public void appendText(String newNote) throws RemoteException {
		buf.append("\n"+newNote);  
	}

	public void clean() throws RemoteException {
		buf = new StringBuffer();
	}

	public String getText() throws RemoteException {
		return buf.toString(); 
	}

	public Object[] getFileList(String currentPath) throws RemoteException {
		File dir = new File(currentPath);
                
                ArrayList<String> fileList = new ArrayList<String>();
                String[] dirList = dir.list();
                
                fileList.add("..");
                for (String item : dirList)
                {
                    fileList.add(item);
                }
                
		return fileList.toArray();
	}

	public String changeDir(String path, String currentPath) throws RemoteException {
                String tmpPath;
                if (currentPath.charAt(currentPath.length()-1) != '/')
                        tmpPath = currentPath + "/" +  path;
                else
                        tmpPath = currentPath + path;
                
                File tmpDir = new File(tmpPath);
                if (tmpDir.exists() && tmpDir.isDirectory() )
                    return tmpPath;
                else
                    return currentPath;
	}

    public byte[] getFile(String fileName, String currentPath) throws RemoteException {
        BufferedInputStream bis = null;
        try {
            String filePath = currentPath + "/" + fileName;
            File file = new File(filePath);
            if (!file.exists() || !file.isFile()) {
                return null;
            }
            bis = new BufferedInputStream(new FileInputStream(file));
            final byte[] bytes = new byte[(int) file.length()];
            bis.read(bytes); 
            bis.close();
            return bytes;
        } catch (IOException ex) {
            Logger.getLogger(NoteBoardImpl.class.getName()).log(Level.SEVERE, null, ex);
            
        } finally {
            try {
                bis.close();
                
            } catch (IOException ex) {
                Logger.getLogger(NoteBoardImpl.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

        return null;
        
    }

    public void sendFile(byte[] file, String fileName, String currentPath) throws RemoteException {
       FileWriter fstream = null;
        try {
            File f;
            String fileFullPath = currentPath + "/" + fileName;
            f = new File(fileFullPath);
            if (!f.exists()) {
                f.createNewFile();
            }
            fstream = new FileWriter(f);
            BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(f));
            out.write(file);
            out.close();
            
            for (Object item : registeredClients)
            {
                ((ClientCallback) item).notifyChange() ;
            }
        } catch (IOException ex) {
            Logger.getLogger(NoteBoardClientGUI.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                fstream.close();
            } catch (IOException ex) {
                Logger.getLogger(NoteBoardClientGUI.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    public synchronized void registerForCallback(ClientCallback clientCallback) throws RemoteException {
        this.registeredClients.add(clientCallback) ;
    }

    public synchronized void  unregisterForCallback(ClientCallback clientCallback) throws RemoteException {
        this.registeredClients.remove(clientCallback) ;
    }
        
        

}
